package cn.gdut.chengcai.base;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;

import org.apache.commons.beanutils.BeanUtils;

import cn.gdut.chengcai.entity.EmailLogInfo;
import cn.gdut.chengcai.entity.EmailTemplateInfo;
import cn.gdut.chengcai.entity.EmailTemplateInfoExample;
import cn.gdut.chengcai.entity.KeyValueInfo;
import cn.gdut.chengcai.enums.EmailTemplateType;
import cn.gdut.chengcai.service.EmailLogInfoService;
import cn.gdut.chengcai.service.EmailTemplateInfoService;
import cn.gdut.chengcai.service.KeyValueInfoService;
import cn.gdut.chengcai.vo.EmailTemplateInfoVo;

public class EmailUtil {

	public static void fillVo(EmailTemplateInfo entity, EmailTemplateInfoVo vo) throws Exception {

		BeanUtils.copyProperties(vo, entity);
		// 邮件模板类型
		if (null != entity.getType()) {
			vo.setTypeIndex(entity.getType());
			vo.setType(EmailTemplateType.getString(Integer.parseInt(entity.getType().toString())));
		}

		// 布尔值
		if (entity.getIsDefault() == true) {
			vo.setIsDefault("是");
		} else {
			vo.setIsDefault("否");
		}

		// 时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		vo.setCreateTime(sdf.format(entity.getCreateTime()));
		vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
	}

	public static void fillVoList(List<EmailTemplateInfo> list, List<EmailTemplateInfoVo> voList) throws Exception {
		if ((null != list) && (list.size() > 0)) {
			for (EmailTemplateInfo entity : list) {
				EmailTemplateInfoVo vo = new EmailTemplateInfoVo();
				fillVo(entity, vo);
				voList.add(vo);
			}
		}
	}

	public static boolean sendSystemMail(EmailLogInfoService emailLogInfoService,
			EmailTemplateInfoService emailTemplateInfoService, KeyValueInfoService keyValueInfoService, String ip,
			Integer type, String addTo, String[] args) throws MessagingException {

		EmailTemplateInfoExample example = new EmailTemplateInfoExample();
		example.createCriteria().andTypeEqualTo(Byte.valueOf(type.toString())).andIsDefaultEqualTo(true)
				.andIsDeletedEqualTo(false);
		List<EmailTemplateInfo> list = emailTemplateInfoService.selectByExample(example);

		if ((null == list) || (list.size() == 0)) {
			return false;
		} else {
			EmailTemplateInfo emailTemplateInfo = list.get(0);
			String content = emailTemplateInfo.getContent();
			for (int i = 0; i < args.length; i++) {
				content = content.replace("#{" + (i + 1) + "}", args[i]);
			}

			Map<String, KeyValueInfo> mapSetting = SystemSettingUtil.getLastestSystemSettingMap(keyValueInfoService);
			MailSmtpUtil mailSmtpUtil = MailSmtpUtil.getNewInstance(
					mapSetting.get(SystemConstantUtil.DEFAULT_MAIL_HOST).getValue(),
					mapSetting.get(SystemConstantUtil.DEFAULT_MAIL_USERNAME).getValue(),
					mapSetting.get(SystemConstantUtil.DEFAULT_MAIL_PASSWORD).getValue());

			mailSmtpUtil.addTo(addTo);
			mailSmtpUtil.setSubject(emailTemplateInfo.getSubject());
			mailSmtpUtil.setContent(content);

			// 发送邮件
			boolean result = mailSmtpUtil.send();

			EmailLogInfo emailLog = new EmailLogInfo();
			emailLog.setType(Byte.valueOf(type.toString()));
			emailLog.setSenderEmail(mapSetting.get(SystemConstantUtil.DEFAULT_MAIL_USERNAME).getValue());
			emailLog.setSenderUsername("admin");
			emailLog.setSenderIp(ip);
			emailLog.setIsSystemMail(true);
			emailLog.setToList(addTo);
			emailLog.setSubject(emailTemplateInfo.getSubject());
			// 邮件内容最好不要存，太长，数据库连接容易中断！！！
			// emailLog.setContent(content);
			emailLog.setIsSuccessed(result);
			emailLog.setIsDeleted(false);
			emailLog.setCreateTime(new Date());
			emailLogInfoService.insert(emailLog);

			return result;
		}
	}
}
